package com.encap;

/**
 * @Author 小闽
 * @Date 2024/8/8 16:52
 * @PackageName: com.encap
 * @ClassName: Enapsulation01
 * @Description: 封装入门
 * @Version 1.0
 */
public class Encapsulation01 {
    /**
     * 请编写一个程序，不能随便查看人的年龄工资等隐私，并对设置的年龄进行合理的验证。年龄合理就
     * 设置，否则就默认
     * 年龄，必须在1-120，年龄，工资不能直接查看，name的长度在2-6个字符之间
     */
    public static void main(String[] args) {
        Person person = new Person();
        person.setName("jack");
        person.setAge(11);
        person.setSalary(10000);
        person.getInfo();

        Person smith = new Person("smith", 200, 10000);
        smith.getInfo();
    }
}

class Person{
    public String name;
    private int age;
    private int salary;

    /**
     * 构造器
     * @return
     */
    public Person(){

    }
    public Person(String name, int age, int salary){
//        this.name = name;
//        this.age = age;
//        this.salary = salary;
        setName(name);
        setAge(age);
        setSalary(salary);
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        if (name.length() > 2 && name.length() < 6){
            this.name = name;
        }else{
            System.out.println("名字长度在 2-6之间");
            this.name = "无名小子";
        }

    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age >=1 && age <= 120){
            this.age = age;
        }else{
            System.out.println("年龄必须在1-120之间");
            this.age = 18;
        }

    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }
    public void getInfo(){
        System.out.println("这个人的信息为：" + "\n" +
                "姓名：" +  name + "\n" + "年龄：" + age + "\n" + "薪水：" + salary);
    }
}